﻿using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;

public class ReadXml
{
    private static ReadXml instace;
    public static ReadXml Instance
    {
        get
        {
            if (instace == null)
            {
                instace = new ReadXml();
            }
            return instace;
        }
    }
    public XmlNodeList ContentNodeList;
    public XmlNode RootNode;
    public ReadXml()
    {

        XmlDocument doc = new XmlDocument();
        doc.Load(Application.streamingAssetsPath + "/Config/Config.xml");
        RootNode = doc.SelectSingleNode("//root");
        ContentNodeList = RootNode.ChildNodes;
    } 
    public XmlNode loadXmlNode(params int[] levels)
    {

        XmlNode currentNode = ContentNodeList[levels[0]];
        for (int i = 1; i < levels.Length; i++)
        {
            if (currentNode.ChildNodes.Count > levels[i])
            {
                currentNode = currentNode.ChildNodes[levels[i]];
            }
            else
            {
                return null; // 处理越界情况
            }
        }
        return currentNode;
    }
    // 示例方法：读取指定层级的节点
    public string Read(params int[] levels)
    {
        XmlNode currentNode = ContentNodeList[levels[0]];

        for (int i = 1; i < levels.Length; i++)
        {
            if (currentNode.ChildNodes.Count > levels[i])
            {
                currentNode = currentNode.ChildNodes[levels[i]];
            }
            else
            {
                return "指定层级不存在"; // 处理越界情况
            }
        }

        // 返回节点的名称或者其他属性
       // return currentNode.Name;
        return currentNode.Attributes["name"].Value;
        //return PrintChildNodes
    }  
    // 打印子节点的属性
    //private string PrintChildNodes(XmlNode node)
    //{
       
    //    foreach (XmlNode child in node.ChildNodes)
    //    {
    //       child.Attributes["name"].Value;
    //    }
    //    return result;
    //}


}